﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;

namespace XMLNameSpaceResearch.Entity
{
    /// <summary>
    /// UserEntity to description a user's infomation on Douban.
    /// Example: Get http://api.douban.com/people/{uid}
    /// </summary>
    public class UserEntity
    {
        //username
        //名称 意义 备注 
        //db:location 用户的长居地 用户可能没有长居地 
        //db:uid 用户在豆瓣上的username          
        //link rel="alternate" 用户的豆瓣页面  
        //link rel="icon" 用户的头像图片链接  
         

        public static WebClient client = new WebClient();

        

        public UserEntity(string uid)
        {
            client.Encoding = Encoding.UTF8;
            XmlDocument doc = new XmlDocument();
            string userAPIAddress = string.Format("http://api.douban.com/people/{0}", uid);
            string UserEntityXml = client.DownloadString(new Uri(userAPIAddress));
            XmlNamespaceManager xmlnm = new XmlNamespaceManager(doc.NameTable);
            xmlnm.AddNamespace("default", "http://www.w3.org/2005/Atom");
            xmlnm.PushScope();
            xmlnm.AddNamespace("db", "http://www.douban.com/xmlns/");
            xmlnm.PushScope();
            xmlnm.AddNamespace("gd", "http://schemas.google.com/g/2005");
            xmlnm.PushScope();
            xmlnm.AddNamespace("openSearch", "http://a9.com/-/spec/opensearchrss/1.0/");

            doc.LoadXml(UserEntityXml);
            
            _userName = doc.SelectSingleNode("//default:title", xmlnm).InnerText;

            _content = doc.SelectSingleNode("//default:content", xmlnm).InnerText;

            XmlNode doubanPageNode = doc.SelectSingleNode("//default:link[@rel='alternate']", xmlnm);
            _doubanPage = doubanPageNode.Attributes["href"].Value;
        }
        //title 用户的名号  
        private string _userName;

        public string UserName
        {
            get
            {
                return _userName;
            }            
        }

        //content 用户的自我介绍  
        private string _content;

        public string Content
        {
            get
            {
                return _content;
            }
        }

        //link rel="homepage" 用户的个人主页 
        private string _doubanPage;

        public string DoubanPage
        {
            get
            {
                return _doubanPage;
            }
        }
       
    }
}
